// pages/coulumnList/coulumnList.js

const app = getApp();
var api = require('../../api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    app.bind();
    console.log(options)
    let str = JSON.parse(options.str);
    this.setData({
      lesson_id: str.id
    })
    this.getDetail();
    console.log(str)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var url = app.getcurrentPageStr();
    wx.setStorageSync("from_url", url);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var page = this;

    page.getDetail();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    // 显示加载图标  
    wx.showLoading({
      title: '玩命加载中',
    });

    if (that.data.page < that.data.count) {
      that.setData({
        page: that.data.page + 1
      });
    }

    that.getDetail();
    // 隐藏加载框  
    wx.hideLoading();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var url = app.getcurrentPageStr();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "&user_id=" + user_id,
      title: '得道1314'
    };
  },

  //获取列表
  getDetail: function () {
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    var that = this;
    wx.request({
      url: api.index.lessonInfo,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        lesson_id: that.data.lesson_id,
        page:that.data.page
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          that.setData({
            list: res.data.data.list,
            info: res.data.data.info,
            row_count: res.data.data.row_count
          })
          console.log(that.data.list)
          console.log(that.data.row_count);
          Math.ceil(that.data.row_count / 10);
          that.setData({
            count: Math.ceil(that.data.row_count / 10)
          })
          console.log(that.data.count)
          
          if (that.data.list.length == 0){
            wx.switchTab({
              url: '/pages/index/index',
            });
            wx.showToast({
              title: '该专栏还没有课程',
              icon: 'none',
            });
          }



          console.log(that.data.info)
        } else if (res.data.code == -1) {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        }
      }
    });

  },

  //跳转到详情
  toCourse: function (e) {
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var proList = this.data.list;
    let str = JSON.stringify(proList[index]);
    console.log(str);
    wx.navigateTo({
      url: '/pages/freeDetail/freeDetail?str=' + str,
    })
  }
})